package org.redr.widgetGUI.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QListWidget;
import com.trolltech.qt.gui.QListWidgetItem;

public class ListBox extends WidgetState{
	
	QListWidget listBox;
	
	public QListWidget widget(){
		return listBox;
	}
	
	public ListBox(LayoutState p, WidgetSettings ws){
		super(p, ws);
		listBox	=	new QListWidget();
		p.addWidget(new QLabel(_getSetting("label", "!!FIX ME LABEL SHOULD BE SET!!")));
		p.addWidget(listBox);
		_loadSettings();
	}
	
	private void _loadSettings(){
		// set the items
		HashMap<String, String>	i	=	settings.items("items").getKeyValuePairs();
		listBox.clear();
		for(String s : i.keySet()){
			listBox.addItem(i.get(s));
		}
		
		HashMap<String, String> si	=	settings.items("selectedItems").getKeyValuePairs();
		for (String s : si.keySet()){
			QListWidgetItem thisItem	=	listBox.item(Integer.parseInt(s));
			if (thisItem != null) thisItem.setSelected(true);
		}
		
		
		
	}
	
	private Integer[] _setListBoxSettings(){
		settings.items("selectedItems").clear();
		ArrayList<Integer> result	=	new ArrayList<Integer>();
		for (int r = 0; r < listBox.count(); r++){
			QListWidgetItem thisItem = listBox.item(r);
			if (thisItem != null){
				if (thisItem.isSelected()) {
					settings.items("selectedItems").addItem(String.valueOf(r), String.valueOf(r));
					result.add(r);
				}
			}
		}
		return (Integer[]) result.toArray();
	}
	
	public void addItem(String key, String value){
		settings.items("items").addItem(key, value);
		listBox.addItem(value);
	}
	
	public Integer[] selectedIndexes(){
		return _setListBoxSettings();
		
	}
	
	public String[] selectedIds(){
		Integer[] 	indexes 	= 	selectedIndexes();
		String[]	ids			=	new String[indexes.length];
		String[]	keys		= 	settings.items("items").getKeys();
		for (int i = 0; i < ids.length; i++){
			ids[i]	=	keys[indexes[i]];
			
		}
		return ids;
	}
	
	public String[] selectedTexts(){
		Integer[] 	indexes 	= 	selectedIndexes();
		String[]	ids			=	new String[indexes.length];
		String[]	keys		= 	settings.items("items").getKeyValuePairs().values().toArray(new String[0]);
		for (int i = 0; i < ids.length; i++){
			ids[i]	=	keys[indexes[i]];
			
		}
		return ids;
	}
	
	public void clear(){
		// clear the widget of all data
		listBox.clear();
		settings.items("items").clear();
		settings.items("selectedItems").clear();
	}
	
	public void update(String[] k_and_v){
		update(k_and_v, k_and_v);
	}
	
	public void update(String[] k, String[] v){
		String[] selected = selectedIds();
		clear();
		for(int i = 0; i < k.length; i++){
			String vi = null;
			if ( i <= v.length ){
				vi = v[i];
			} else {
				vi = k[i];
			}
			addItem(k[i], vi);
			for (String s : selected){
				if ( k[i].equals(s) ){
					listBox.item(listBox.currentRow()-1).setSelected(true);
					break;
				}
			}
		}
	}

	public void addItems(Map<String, String> items) {
		// TODO Auto-generated method stub
		
	}

	public void update(Map<String, String> items) {
		String[] selected = selectedIds();
		this.clear();
		for (String k : items.keySet()){
			this.addItem(k, items.get(k));
			for (String sel : selected){
				if (sel.equals(k)){
					this.listBox.item(this.listBox.currentRow()-1).setSelected(true);
					break;
				}
			}
		}
	}

}
